package com.tceasy.gateway.api;

import com.alibaba.druid.util.StringUtils;
import com.tceasy.common.utils.date.DateUtils;
import com.tceasy.common.utils.json.JsonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by zhanghuan on 2016/3/31.
 */
public class Response {
    Logger logger= LoggerFactory.getLogger(Response.class);

    String returnCode;
    String returnMsg;
    private Map result;

    public Response() {
        result=new HashMap();
    }

    public Response(String returnCode, String returnMsg) {
        this.returnCode = returnCode;
        this.returnMsg = returnMsg;
        result=new HashMap();
        result.put("returnCode",returnCode);
        result.put("returnMsg", returnMsg);
        result.put("timestamp", DateUtils.currentDatetime());
    }

    public static Response SUCCESS(){
        return new Response("T","OK");
    }
    public static Response SUCCESS(String msg){
        return new Response("T",msg);
    }
    public static Response BUSINESS_FAIL(){
        return  new Response("1","业务级失败");
    }
    public static Response BUSINESS_FAIL(String msg){
        return  new Response("1",msg);
    }
    public static Response SYSTEM_FAIL(){
        return  new Response("2","服务器端异常");
    }
    public static Response SYSTEM_FAIL(String msg){
        return  new Response("2",msg);
    }
    /**
     * 开放平台成功响应
     * @return
     */
    public static Response success(){
        return new Response("T","OK");
    }

    /**
     * 开放平台失败响应
     * @param msg
     * @return
     */
    public static Response fail(String msg){
        return new Response("F",msg);
    }

    public Response msg(String msg){
        return put("returnMsg",msg);
    }

    /**
     * 向result中添加额外的参数
     * @param key
     * @param value
     * @return
     */
    public Response put(String key,Object value){
        result.put(key,value);
        return this;
    }

    public Response putAll(Map map){
        if(map!=null){
            result.putAll(map);
        }
        return this;
    }

    public Object get(String key){
        return result.get(key);
    }

    /*判断是否成功*/
    public boolean isSuccess(){
        return StringUtils.equals(returnCode, "T");
    }

    public String toJson(){
        return JsonUtil.toJsonWithString(result);
    }

    public Map getResult() {
        return result;
    }
}
